#!/usr/bin/python
import pygame
from pygame.locals import *
from cube_render import *

#################################
#################################
##   ------> i
##   |\
##   | \ k
## j V  v
##
##     H ____________ G
##      |\           |\
##      | \ _________|_\ C
##      | D|         |  |
##      |  |         |  |
##     E|__|________F|  |
##       \ |          \ |
##        \|___ _______\|
##        A              B
##
##################################
##################################



class Cobra:
	def __init__(self, points_of_cube):
		self.points = points_of_cube

		self.point_x = 4
		self.point_y = 0
		self.point_z = 4

		self.axis_x = self.points[self.point_x]
		self.axis_y = self.points[self.point_y]
		self.axis_z = self.points[self.point_z]

		self.pos_axis_x = 5
		self.pos_axis_y = 4
		self.pos_axis_z = 5

		self.x = self.axis_x[self.pos_axis_x][0]
		self.y = self.axis_y[self.pos_axis_y][1]
		self.z = self.axis_z[self.pos_axis_z][2]

		self.pos = [
		[(self.x, self.y, self.z), (self.x + 30, self.y, self.z)],
		[(self.x + 30, self.y, self.z), (self.x + 30, self.y + 30, self.z)],
		[(self.x + 30, self.y + 30, self.z), (self.x, self.y + 30, self.z)],
		[(self.x, self.y + 30, self.z), (self.x, self.y, self.z)],
		]

		self.body = [self.pos]

	def change_pos(self):
		x = self.points[self.point_x]
		y = self.points[self.point_y]
		z = self.points[self.point_z]

		self.pos = [
		[(self.x, self.y, self.z), (self.x + 30, self.y, self.z)], #(self.x, self.y, self.z)],
		[(self.x+30, self.y, self.z), (self.x+30, self.y+30, self.z)],
		[(self.x+30, self.y+30, self.z), (self.x, self.y+30, self.z)],
		[(self.x, self.y+30, self.z), (self.x, self.y, self.z)],
		]
		self.body = [self.pos]

		#draw_3dline()
	def atualizar(self, points_of_cube, direction):
		self.points = points_of_cube
		#print self.points[self.axis_x]
		if direction == "R":
			self.pos_axis_x += 1

		elif direction == "L":
			self.pos_axis_x -= 1

		elif direction == "D":
			self.pos_axis_y -= 1

		elif direction == "U":
			self.pos_axis_y += 1

		print self.x, self.y, self.z
		self.x = self.points[self.point_x][self.pos_axis_x][0]
		self.y = self.points[self.point_y][self.pos_axis_y][1]
		self.z = self.points[self.point_z][self.pos_axis_z][2]
		print self.x, self.y, self.z

		self.change_pos()

def main():
	pygame.init()
	screen = pygame.display.set_mode((640, 480))
	ORIGINX = screen.get_width()/2
	ORIGINY = screen.get_height()/2

	# Move origin to center of screen
	cube = [(-150,150,150), (150,150,150), (150,-150,150), (-150,-150,150),
			(-150,150,-150), (150,150,-150), (150,-150,-150), (-150,-150,-150)]

	#linhas horizontais
	point_in_ad = [(cube[0][0], cube[0][1] - j, cube[0][2]) for j in range(0, 301, 30)]
	point_in_bc = [(cube[1][0], cube[1][1] - j, cube[1][2]) for j in range(0, 301, 30)]
	point_in_fg = [(cube[5][0], cube[5][1] - j, cube[5][2]) for j in range(0, 301, 30)]
	point_in_eh = [(cube[4][0], cube[4][1] - j, cube[4][2]) for j in range(0, 301, 30)]

	#linhas verticais
	point_in_ab = [(cube[0][0] + i, cube[0][1], cube[0][2]) for i in range(0, 301, 30)]
	point_in_bf = [(cube[1][0], cube[1][1], cube[1][2] - k) for k in range(0, 301, 30)]
	point_in_fe = [(cube[5][0] - i, cube[5][1], cube[5][2]) for i in range(0, 301, 30)]
	point_in_ea = [(cube[4][0], cube[4][1], cube[4][2] + k) for k in range(0, 301, 30)]

	point_in_dc = [(cube[3][0] + i, cube[3][1], cube[3][2]) for i in range(0, 301, 30)]
	point_in_cg = [(cube[2][0], cube[2][1], cube[2][2] - k) for k in range(0, 301, 30)]
	point_in_gh = [(cube[6][0] - i, cube[6][1], cube[6][2]) for i in range(0, 301, 30)]
	point_in_hd = [(cube[7][0], cube[7][1], cube[7][2] + k) for k in range(0, 301, 30)]

	#####
	points_of_cube = [
	point_in_ad, point_in_bc, point_in_fg, point_in_eh,
	point_in_ab, point_in_bf, point_in_fe, point_in_ea,
	point_in_dc, point_in_cg, point_in_gh, point_in_hd,
	]

	matriz_de_posicao = [[0 for j in range(len(i))] for i in points_of_cube]

	cube_lines = [
	(point_in_ad, point_in_bc),
	(point_in_bc, point_in_fg),
	(point_in_fg, point_in_eh),
	(point_in_eh, point_in_ad),

	(point_in_ab, point_in_dc),
	(point_in_bf, point_in_cg),
	(point_in_fe, point_in_gh),
	(point_in_ea, point_in_hd),
	]

	cobra = Cobra(points_of_cube)
	drawer = Drawer(screen, ORIGINX, ORIGINY)
	rotator = Rotator()
	while 1:
		#Erase the screen
		screen.fill((0, 0, 0))

		#Draw in screen
		drawer.draw_cube(screen, (0, 0, 255), cube)

		for vetor in cube_lines:
			drawer.draw_cube_lines(screen, (0, 0, 255), vetor[0], vetor[1])

		for square in cobra.body:
			drawer.draw_square(screen, (0, 255, 0), square)

		#Get events
		for e in pygame.event.get():
			if e.type == QUIT:
				exit()

			if e.type == KEYDOWN:
				if e.key == K_ESCAPE:
					break

				if e.key == K_UP:
					rotator.rotate_cube(cube, points_of_cube, "U")
					#cobra.atualizar(points_of_cube, "U")

				elif e.key == K_LEFT:
					rotator.rotate_cube(cube, points_of_cube, "L")
					#cobra.atualizar(points_of_cube, "L")

				elif e.key == K_DOWN:
					rotator.rotate_cube(cube, points_of_cube, "D")
					#cobra.atualizar(points_of_cube, "D")

				elif e.key == K_RIGHT:
					rotator.rotate_cube(cube, points_of_cube, "R")
					#cobra.atualizar(points_of_cube, "R")

		pygame.display.flip()


if __name__ == "__main__":
	main()
